package com.atkxp.filter;

import com.atkxp.domain.User;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author user
 * @declaration
 * @create 2022/10/6-13:15
 */
public class ManagerFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        servletResponse.setCharacterEncoding("UTF-8");
        User user = (User) request.getSession().getAttribute("user");
        if(user != null && "root".equals(user.getUsername()) && "longhunpomie".equals(user.getPassword())){
            filterChain.doFilter(servletRequest, servletResponse);
        }else{
            System.out.println("没有权限, 请联系管理员获得权限");
            response.sendRedirect(request.getContextPath() + "/pages/client/errorPage.jsp");
//            request.getRequestDispatcher("/index.jsp").forward(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
